﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Lib.Utils;
using Lib.Bases;

namespace MS.INV.Prices
{
    public partial class PrimeCost : Lib.Bases.Forms.BaseForm
    {
        SysObj.Bg_GiaNhap order;
        DataTable DT_HH = new DataTable();

        public PrimeCost()
        {
            InitializeComponent();

            order = new SysObj.Bg_GiaNhap();

            barDate.EditValue = DateTime.Now;
        }

        protected override void OnHandleDestroyed(EventArgs e)
        {
            order.Dispose();
            base.OnHandleDestroyed(e);
        }

        private void PrimeCost_Load(object sender, EventArgs e)
        {
            try
            {
                using (SysObj.L_HangHoa hh = new SysObj.L_HangHoa())
                {
                    DT_HH = hh.LoadData();
                    repHH.SetDataSource(DT_HH, "MaHang,TenHang#Mã hàng,Tên hàng");
                }
            }
            catch (Exception ex)
            {
                Lib.Bases.SysError.RaiseError(ex, this.Name, "Load data");
            }

            LoadTree();
        }

        private void LoadTree()
        {
            try
            {
                tree.Nodes.Clear();

                DataTable DT = order.GetHistory();
                if (DT != null && DT.Rows.Count > 0)
                {
                    foreach (DataRow row in DT.Rows)
                    {
                        TreeNode tn = new TreeNode(Convert.ToDateTime(row["Ngay"]).ToString("dd/MM/yyyy"));
                        tn.Tag = Convert.ToDateTime(row["Ngay"]);
                        tree.Nodes.Add(tn);
                    }
                }
            }
            catch (Exception ex)
            {
                Lib.Bases.SysError.RaiseError(ex, this.Name, "Load data");
            }
        }

        private void barDate_EditValueChanged(object sender, EventArgs e)
        {
            try
            {
                order.ReadData(Convert.ToDateTime(barDate.EditValue));
                txtDescr.Text = order.GhiChu;

                grid.DataSource = order.GetDetail();
            }
            catch (Exception ex)
            {
                Lib.Bases.SysError.RaiseError(ex, this.Name, "Load data");
            }
        }

        private void barUpdate_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                DataTable DT = grid.GetAcceptChanges();
                if (DT == null || DT.Rows.Count <= 0)
                {
                    Msg.ErrOK("Vui lòng nhập chi tiết đơn giá từng thực đơn trước khi cập nhật.");
                    return;
                }

                order.Ngay = Convert.ToDateTime(barDate.EditValue);
                order.GhiChu = txtDescr.Text;
                order.D_DT = DT;

                order.OnUpdate();

                barUpdate.Enabled = false;
                LoadTree();
            }
            catch (Exception ex)
            {
                Lib.Bases.SysError.RaiseError(ex, this.Name, "Update data");
            }
        }

        private void barDel_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (MessageBox.Show("Bạn có chắc muốn xóa bảng giá thực đơn này không ?", "Xác nhận ?",
                MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.No)
                return;

            try
            {
                order.OnDelete();
                barDate_EditValueChanged(null, new EventArgs());
                LoadTree();
            }
            catch (Exception ex)
            {
                Lib.Bases.SysError.RaiseError(ex, this.Name, "Delete data");
            }
        }

        private void barClose_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.Close();
        }

        private void tree_AfterSelect(object sender, TreeViewEventArgs e)
        {
            barDate.EditValue = Convert.ToDateTime(e.Node.Tag);
        }

        private void view_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {
            barUpdate.Enabled = true;

            if (e.Column.FieldName.ToLower() == "idhanghoa")
            {
                DataRow row = view.GetFocusedDataRow();
                if (object.Equals(row, null)) return;
                DataRow[] rS = DT_HH.Select("IdHangHoa=" + Cvert.ToInt32(row["IdHangHoa"], 0));
                if (!object.Equals(rS, null) && rS.Length > 0)
                {
                    row["MaHang"] = Cvert.ToString(rS[0]["MaHang"], "");
                }
            }
        }

        private void txtDescr_TextChanged(object sender, EventArgs e)
        {
            barUpdate.Enabled = true;
        }
    }
}
